import { Cell } from './cell';
import { Select } from './select';
import { Config } from './config';
import "reflect-metadata";
import { Injectable, InjectFactory, TFactory } from '../decorators/inject';
import { NFill, NFont } from '../types/style.interface';
import { Hook } from './hook';
import { Transform, toJSON, classTransformToObj, objTransformToClass } from '../decorators/toJSON';
import { AddHook } from '../decorators/add-hook';
@Injectable()
@Transform()
export class Row {
    public position: number = 0;
    @toJSON(false)
    public isManualHeight: boolean = false;
    @toJSON({})
    protected fill: NFill.IFill = {};
    @toJSON({})
    protected font: NFont.IFont = {};
    @toJSON(null)
    protected height: number = null;
    protected _height: number; // 动态的计算后的值
    @toJSON([], {
        toObj: (cells: Cell[]) => {
            let result: any[] = [];
            for(let i = 0; i < cells.length; i++) {
                if(cells[i]) {
                    let cell = classTransformToObj(cells[i]) as Object;
                    if(Object.keys(cell).length !== 0) {
                        // @ts-ignore;
                        cell.col = i;
                        result.push(cell);
                    }
                }
            }
            return result;
        },
        toCls: (instance: {factory: any}, cells: any[]) => {
            let result: any[] = [];
            for(let cell of cells) {
                let cellInstance = instance.factory(Cell);
                objTransformToClass(cellInstance, cell);
                result[cell.col] = cellInstance;
            }
            return result;
        }
    })
    public cells: Cell[] = [];
    constructor(
        protected conf: Config,
        protected hook: Hook,
        @InjectFactory() public factory: TFactory
        ) {
        this._height = this.conf.header.row.height / this.conf.pixelRatio / this.conf.scale;
    }
    @AddHook('pixelRatioChange')
    @AddHook('afterScaleChange')
    protected defaultHeightChange() {
        this._height = this.conf.header.row.height / this.conf.pixelRatio / this.conf.scale;
    }
    public init() {
        
    }
    public getHeight() {
        if(this.isManualHeight == true) {
            return this.height * this.conf.enlarge;
        } else {
            return this._height * this.conf.enlarge;
        }
    }
    public setHeight(height: number) {
        this.hook.beforeRowHeightChange.excute(height);
        this.isManualHeight = true;
        this.height = height;
        this.hook.afterRowHeightChange.excute(height);
    }
    public getActualHeight() {
        if(this.isManualHeight == true) {
            return this.height * this.conf.enlarge;
        } else {
            return this._height * this.conf.enlarge;
        }
    }
    /**
     * @description 动态计算行高
     */
    public calculateHeight() {
        if(this.isManualHeight == true) {
            return;
        }
        let maxHeight = this.conf.header.row.height;
        for(let cell of this.cells) {
            let temp = cell.calculateHeight();
            if(temp > maxHeight) {
                maxHeight = temp;
            }
        }
        this._height = maxHeight;
    }
    public createCell() {

    }
    public colIsExist(col: number) {
        return Boolean(this.cells[col]);
    }
    public getCell(col: number) {
        if(this.cells[col] == undefined) {
            let cell = this.factory(Cell) as Cell;
            cell.updatePosition(this.position, col);
            return cell;
        }
        return this.cells[col];
    }
    public hasCol(col: number) {
        return Boolean(this.cells[col]);
    }
    public getMaxCol() {
        return this.cells.length;
    }
    public setCell(col: number, cell: Cell) {
        this.cells[col] = cell;
        this.hook.tableDataChange.excute();
    }
    public setCellValue(col: number, value: BaseValueType) {
        if(this.cells[col]) {
            this.cells[col].value.updateOriginValue(value);
        } else {
            let cell = this.factory(Cell);
            cell.updatePosition(this.position, col);
            cell.value.updateOriginValue(value);
            this.cells[col] = cell;
        }
    }
    public getFill() {
        return this.fill;
    }
    public setFill() {
        
    }
    public deleteCol(col: number) {
        if(this.colIsExist(col) === true) {
            
        } else {

        }
    }
    public getFont() {
        return this.font;
    }
    public setFont() {
        
    }
    public updatePosition(row: number) {
        this.position = row;
    }

    // public deleteRows(rows: number[]) {
    //     let result: {[key: string]: any} = {};
    //     for(let row of rows) {
    //         this.hook.beforeDeleteRow.excute(row, this);
    //         if(row > this)
    //         this.hook.afterDeleteRow.excute(row, this);
    //     }
    // }
}
